package com.omt.tarjimdinek.domain.poll.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.apache.log4j.Logger;

import com.google.common.base.Objects;
import com.omt.tarjimdinek.domain.islamicLibrary.entity.Sahabi;
import com.omt.tarjimdinek.domain.member.entity.Membre;
import com.omt.tarjimdinek.domain.translate.entity.HistoriqueCorrection;
import com.omt.tarjimdinek.domain.translate.entity.Ligne;
import com.omt.tarjimdinek.domain.translate.entity.LigneTraduction;
import com.omt.tarjimdinek.support.Identifiable;

/**
 * poll of members
 * @author chabane
 */
@Entity
@Table(name="a01_vt")
public class Vote implements Identifiable<Long>, Serializable{

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(Ligne.class);
    
	/** ID SGBD */
	@Column(name="CNVT", length=11)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(name = "VERSION", precision = 10)
    @Version
    private Integer version;
	
	/** translation line */
	@NotNull
	@JoinColumn(name="CNLGNTRD")
	@ManyToOne
	private LigneTraduction ligneTraduction;
	
	/** history of correction */
	@NotNull
	@JoinColumn(name="CNHSTCOR")
	@ManyToOne
	private HistoriqueCorrection historiqueCorrection;
	
	/** member */
	@NotNull
	@JoinColumn(name="CNMBR", nullable=false)
	@ManyToOne
	private Membre voteur;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public LigneTraduction getLigneTraduction() {
		return ligneTraduction;
	}

	public void setLigneTraduction(LigneTraduction ligneTraduction) {
		this.ligneTraduction = ligneTraduction;
	}

	public HistoriqueCorrection getHistoriqueCorrection() {
		return historiqueCorrection;
	}

	public void setHistoriqueCorrection(HistoriqueCorrection historiqueCorrection) {
		this.historiqueCorrection = historiqueCorrection;
	}

	public Membre getVoteur() {
		return voteur;
	}

	public void setVoteur(Membre voteur) {
		this.voteur = voteur;
	}
	
	@Transient
	public boolean isIdSet() {
		return id != null && id.compareTo(Long.valueOf(0)) != 0;
	}
	
	/**
     * equals implementation using a business key.
     */
    public boolean equals(Object other) {
        return this == other || (other instanceof Sahabi && hashCode() == other.hashCode());
    }

    public int hashCode() {
        int hashCode = Objects.hashCode(getId());
        return hashCode;
    }
    
    /**
     * Construct a readable string representation for this instance.
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return Objects.toStringHelper(this) //
                .add("id", getId()) //
                .add("correcteur", getHistoriqueCorrection()) //
                .add("ligne", getLigneTraduction()) //
                .add("voteur", getVoteur()) //
                .add("version", getVersion()) //
                .toString();
    }
}
